package com.wsm.blog.controller;

import com.wsm.blog.domain.ContentDO;
import com.wsm.blog.service.ContentService;
import com.wsm.common.annotation.DuplicateSubmitToken;
import com.wsm.common.enums.BlogEnum;
import com.wsm.common.enums.DictEnum;
import com.wsm.common.service.DictService;
import com.wsm.common.utils.DateUtils;
import com.wsm.common.utils.PageUtils;
import com.wsm.common.utils.Query;
import com.wsm.common.utils.Result;
import com.wsm.system.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author wangshimin
 */

@Api(tags = "博客主页")
@RequestMapping("/blog")
@Controller
public class BlogController {
	@Autowired
	ContentService bContentService;
	@Autowired
	UserService userService;
	@Autowired
	DictService dictService;

	@GetMapping()
	String blog(Model model) {
		model.addAttribute("display", true);
		return "blog/index/main";
	}

	@ResponseBody
	@GetMapping("/open/list")
	@DuplicateSubmitToken(expire = 2, time = TimeUnit.MINUTES)
	public Result<PageUtils> opentList(@RequestParam Map<String, Object> params) {
		params.put("status", BlogEnum.Blog_Status.Released.getStatus());
		params.put("delFlag", 1);
		Query query = new Query(params);
		List<ContentDO> bContentList = bContentService.listNotHaveContent(query);
		int total = bContentService.count(query);
		PageUtils pageUtils = new PageUtils(bContentList, total);
		return Result.success(pageUtils);
	}

	@GetMapping("/open/post/{cid}")
	String post(@PathVariable("cid") Long cid, Model model) {
		ContentDO bContentDO = bContentService.get(cid);
		model.addAttribute("bContent", bContentDO);
		model.addAttribute("gtmModified", DateUtils.format(bContentDO.getGtmCreate(), DateUtils.DATE_TIME_PATTERN));
		model.addAttribute("type", dictService.getTypeDO(DictEnum.Dict_Type.Blog_type.getType(), bContentDO.getCategories()));
		return "blog/index/post";
	}
	@GetMapping("/open/page/{categories}")
	String about(@PathVariable("categories") String categories, Model model) {
		Map<String, Object> map = new HashMap<>(16);
		map.put("categories", categories);
		ContentDO bContentDO = null;
		if (bContentService.list(map).size() > 0) {
			bContentDO = bContentService.list(map).get(0);
		}
		model.addAttribute("bContent", bContentDO);
		return "blog/index/post";
	}

	@GetMapping("/open/author/{created}")
	String created(@PathVariable("created") Long created, Model model) {
		model.addAttribute("display", true);
		//获取作者信息
		model.addAttribute("author", userService.get(created));
		return "blog/index/author";
	}

	@GetMapping("/open/type/{categories}")
	String categories(@PathVariable("categories") String categories, Model model) {
		model.addAttribute("display", true);
		//获取作者信息
		model.addAttribute("dict", dictService.getTypeDO(DictEnum.Dict_Type.Blog_type.getType(),categories));
		return "blog/index/type";
	}

	@GetMapping("/about")
	public String about() {
		return "blog/index/about";
	}
}
